package org.feng.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.feng.entity.FormPeople;
import org.feng.entity.Student;
import org.feng.service.StudentService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * 学生控制器
 *
 * @version v1.0
 * @author: fengjinsong
 * @date: 2022年06月11日 11时14分
 */
@Api(tags = "测试控制器")
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @ApiOperation(value = "测试控制器-测试方法，通过ID获取学生数据", notes = "暂无描述")
    @GetMapping("/{id}")
    public Mono<Student> getOneById(@PathVariable("id") @ApiParam(value = "学生ID") Integer id) {
        return studentService.getById(id);
    }

    @GetMapping("/name/{merchantKey}/{name}")
    public Flux<Student> getStudentsByName(@PathVariable("merchantKey") String merchantKey, @PathVariable("name") String name) {
        return studentService.getStudentsByName(merchantKey, name);
    }

    @PostMapping("/")
    public Mono<Student> saveStudent(@RequestBody Student student) {
        log.info("要保存的学生信息：{}", student);
        return studentService.saveStudent(student);
    }

    @PostMapping("/saves")
    public Flux<Student> saveStudentList(@RequestBody List<Student> studentList) {
        log.info("要保存的学生列表信息：{}", studentList);
        return studentService.saveStudent(studentList);
    }

    @DeleteMapping("/{id}")
    public Mono<Void> deleteStudent(@PathVariable("id") Integer id) {
        return studentService.deleteStudent(id);
    }

    @GetMapping("/select/{subName}/{currentPage}")
    public Flux<Student> selectStudent(@PathVariable("currentPage") Integer currentPage, @PathVariable("subName") String subName) {
        return studentService.selectStudent(currentPage, subName);
    }

    @PostMapping("/postSingle")
    public Mono<Student> postSingleData(@RequestBody Student student) {
        student.setName(student.getName() + Instant.now());
        return Mono.just(student);

    }

    /**
     * 设置 form 提交数据， POST 请求
     *
     * @param studentName 学生姓名
     * @param namePrefix  姓名前缀
     * @return 设置名称后的学生信息
     */
    @PostMapping(value = "/postForm", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public Mono<Student> postFormData(@RequestParam(name = "studentName") String studentName, @RequestParam(name = "namePrefix") String namePrefix) {
        Student student = new Student();
        student.setName(namePrefix + studentName);
        return Mono.just(student);
    }


    @PostMapping(value = "/postRequestBody/{age}")
    public Mono<Student> postRequestBody(@RequestBody Student student, @PathVariable Integer age) {
        student.setName(student.getName() + Instant.now().toEpochMilli());
        student.setAge(age);
        return Mono.just(student);
    }

    @PostMapping(value = "/postRequestBodyWithParam")
    public Mono<Student> postRequestBodyWithParam(@RequestBody Student student, @RequestParam("id") Integer id) {
        student.setName(student.getName() + Instant.now().toEpochMilli());
        student.setId(id);
        return Mono.just(student);
    }

    @PostMapping(value = "/formRequest1", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public Mono<Student> formRequest1(@RequestParam("name") String name, @RequestParam("id") Integer id) {
        Student student = new Student();
        student.setName(name + Instant.now().toEpochMilli());
        student.setId(id);
        return Mono.just(student);
    }


    @PostMapping(value = "/formRequest2", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public Mono<Student> formRequest2(@ModelAttribute("formPeople") FormPeople formPeople) {
        Student student = new Student();
        // 调试异常
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new RuntimeException("发生运行时异常" + e.getMessage());
        }

        student.setName(formPeople.getName() + Instant.now().toEpochMilli());
        student.setId(Integer.parseInt(formPeople.getId()));
        return Mono.just(student);
    }
}
